//
//  CJRewardVideoGMAdapter.m
//  CJModileGMAdapter
//
//  Created by shanghailingye on 2024/4/24.
//

#import "CJRewardVideoGMAdapter.h"

@interface CJRewardVideoGMAdapter()<CJRewardVideoAdDelegate>

@property (nonatomic, strong) CJRewardVideoAd *rewardVideo;

@end

@implementation CJRewardVideoGMAdapter

- (BUMMediatedAdStatus)mediatedAdStatus {
    return BUMMediatedAdStatusNormal;
}

- (void)loadRewardedVideoAdWithSlotID:(nonnull NSString *)slotID andParameter:(nonnull NSDictionary *)parameter {
    self.rewardVideo = [[CJRewardVideoAd alloc] initWithResourceId:slotID userId:@"userId" extend:@""];
    self.rewardVideo.delegate = self;
    [self.rewardVideo loadAdData];
}

- (BOOL)showAdFromRootViewController:(UIViewController * _Nonnull)viewController parameter:(nonnull NSDictionary *)parameter {
    [self.rewardVideo showFromRootViewController:viewController];
    return YES;
}

- (void)didReceiveBidResult:(BUMMediaBidResult *)result {
    // 在此处理Client Bidding的结果回调
}


#pragma mark CJRewardVideoAdDelegate
- (void)rewardVideoOnRewarded:(CJRewardVideoAd *)rewardAd requestId:(NSString *)requestId {
    [self.bridge rewardedVideoAd:self didServerRewardSuccessWithInfo:^(BUMAdapterRewardAdInfo * _Nonnull info) {
        info.verify = true;
    }];
}

- (void)rewardVideoAdDidLoad:(CJRewardVideoAd *)rewardAd resourceId:(NSString *)resourceId {
    NSInteger cpm = [self.rewardVideo getECpm];
    NSDictionary *ext = @{
        BUMMediaAdLoadingExtECPM : cpm ? [NSString stringWithFormat:@"%ld", cpm] : @"0",
    };
    [self.bridge rewardedVideoAd:self didLoadWithExt:ext];
}

- (void)rewardVideoAdOnShow:(CJRewardVideoAd *)rewardAd {
    [self.bridge rewardedVideoAdDidVisible:self];
}

- (void)rewardVideoAdLoadFailed:(CJRewardVideoAd *)rewardAd error:(NSError *)error {
    [self.bridge rewardedVideoAd:self didLoadFailWithError:error ext:@{}];
}

- (void)rewardVideoAdOnClicked:(CJRewardVideoAd *)rewardAd {
    [self.bridge rewardedVideoAdDidClick:self];
}

- (void)rewardVideoAdOnClosed:(CJRewardVideoAd *)rewardAd {
    [self.bridge rewardedVideoAdDidClose:self];
}

@end
